<template>
	<!--  自定义导航页面-->
	<view class="tabBar">
		<u-tabbar :value="current" @change="tabChange" :fixed="true" :placeholder="true" :safeAreaInsetBottom="true">
			<u-tabbar-item v-for="(item,index) in list" :key="index" :text="item.text" :icon="item.iconPath"
				:name="index" :badge="item.badge" />
		</u-tabbar>
	</view>
</template>
<script>
	export default {
		name: "tabBar",
		props: {
			current: {
				type: Number,
				default: 0
			}
		},
		onLoad() {

		},
		data() {
			return {
				list: [{
						"pagePath": "pages/index/index",
						"text": "主页",
						"iconPath": "home",
					},
					{
						"pagePath": "pages/todo/todo",
						"text": "待办",
						"iconPath": "checkmark",
						"badge": 0
					},
					{
						"pagePath": "pages/report/report",
						"text": "统计",
						"iconPath": "file-text",
					},
					{
						"pagePath": "pages/profile/profile",
						"text": "我的",
						"iconPath": "account",
					}
				]
			}
		},
		created() {
			this.fetchBadgeCount();
		},

		methods: {
			tabChange(e) {
				uni.switchTab({
					url: '/' + this.list[e].pagePath
				})
			},

			async fetchBadgeCount() {
				try {
					var apikey = '48ddc0b3-cbc4-2f33-004f-6c02cdb00c83'
					// 调用API获取待办数量
					const res = await this.$commonUnits.request(
						"runtime/task/v1.0/tasks/taskCount", 'POST', {
							"owner": this.$commonUnits.getUserId(),
							"searchList": [],
							"stateList": [
								1,
								2
							],
							"pageable": true,
							"pageIndex": 0,
							"pageSize": 20,
							"categoryIdList": [],
							"onlyIncludeApprovalTask": true,
							"onlyIncludeReadTask": false
						},
						apikey
					);

					if (res.statusCode == 200) {
						this.list[1].badge = res.data || 0;
					}
				} catch (err) {
					console.error('获取待办数量失败:', err);
				}
			}

		}
	}
</script>

<style scoped lang="scss">
	.tabBar {
		pointer-events: auto;
		position: absolute;
	}
</style>